package com.hu.example.service.impl;

import com.hu.core.util.StringUtil;
import com.hu.example.domain.dao.UserDao;
import com.hu.example.domain.model.User;
import com.hu.example.exception.ParameterException;
import com.hu.example.exception.ServiceException;
import com.hu.example.service.UserService;

public class UserServiceImpl implements UserService {
    public UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public User login(String userName, String password) throws ParameterException, ServiceException {
        ParameterException parameterException = new ParameterException();
        if (StringUtil.isEmpty(userName)) {
            parameterException.addErrorFields("errorUserName", "User Name is required");
        }

        if (StringUtil.isEmpty(password)) {
            parameterException.addErrorFields("errorPassword", "Password is required");
        }

        if (!parameterException.getErrorFields().isEmpty()) {
            throw parameterException;
        }

        //userDao = (UserDao) BeanFactory.GetInstance().getBean("userDao");
        User user = userDao.getUserByName(userName);
        if (null == user) {
            throw new ServiceException(1000, "User name not exist");
        }

        if (!password.equals(user.getPassword())) {
            throw new ServiceException(1001, "Password error");
        }

        return user;
    }
}
